﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Media;
using System.Xml.Linq;
using Fireball.Extensions;

namespace Fireball.OpenXML.Word
{

    public enum BorderKind
    {
        Left,
        Right,
        Top,
        Bottom
    }
    /// <summary>
    /// The border for a cell
    /// </summary>
    public sealed class TableBorder : OpenXMLEntity
    {

        /// <summary>
        /// The space between the border and the content
        /// </summary>
        public int? BorderSpace
        {
            get;
            set;
        }

        /// <summary>
        /// The size of the border
        /// </summary>
        public int BorderSize
        {
            get;
            set;
        }

        /// <summary>
        /// If this is True the BorderColor
        /// Property is ignored and used an automatic color
        /// </summary>
        public bool AutoBorderColor
        {
            get;
            set;
        }

        /// <summary>
        /// The border color
        /// </summary>
        public Color BorderColor
        {
            get;
            set;
        }

        [DefaultValue(BorderStyles.None)]
        public BorderStyles Style
        {
            get;
            set;
        }

        /// <summary>
        /// The kind of the border
        /// </summary>       
        [DefaultValue(BorderKind.Left)]
        internal BorderKind BorderKind
        {
            get;
            set;
        }

        public TableBorder()
        {
        }

        public override System.Xml.Linq.XContainer ToXml()
        {
            if (this.Style == BorderStyles.None) return null;

            XElement xBorder = new XElement(NS.WM + this.BorderKind.ToString().ToLower());

            xBorder.Add(new XAttribute(NS.WM + "val", this.Style.ToOffice()));

            xBorder.Add(new XAttribute(NS.WM + "sz", this.BorderSize));

            if (this.BorderSpace != null)
                xBorder.Add(new XAttribute(NS.WM + "space", this.BorderSpace.Value));
            if (!this.AutoBorderColor)
                xBorder.Add(new XAttribute(NS.WM + "color", this.BorderColor.ToHexFormat()));
            else
                xBorder.Add(new XAttribute(NS.WM + "color", "auto"));

            return xBorder;
        }
    }
}
